package com.ckzp.jfinal.system.util;

import com.ckzp.jfinal.system.model.SysOnline;
import com.ckzp.core.util.DateUtil;
import com.ckzp.core.util.StringUtil;
import com.ckzp.jfinal.system.model.SysUser;
import com.ckzp.plugins.ip.IpKit;
import com.jfinal.plugin.activerecord.Db;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 51594 on 2022/7/27.
 */
public class SysOnlineUtil {


    public static Map DOTIME = new HashMap();

    /**
     * @return
     */
    public static SysOnlineUtil n() {
        return new SysOnlineUtil();
    }

    /**
     * @param empid
     * @param ip
     */
    public void saveLogin(String sessid, int empid, String ip, String os, String browser) {
        SysOnline online = SysOnline.dao.findById(sessid);
        String login_addr = IpKit.n().getAddress(ip);
        long times = DateUtil.getYYYYMMHHMMSS();
        if (online == null) {
            SysUser user = SysUser.dao.findById(empid);
            if (user != null) {
                online = new SysOnline().set("emp_id", empid)
                        .set("sess_id", sessid)
                        .set("login_os", os)
                        .set("login_browser", browser)
                        .set("login_addr", login_addr)
                        .set("login_id", user.mod.getStr("login_id"))
                        .set("user_name", user.mod.getStr("user_name"));
                online.set("last_login", new Date()).set("login_ip", ip)
                        .set("last_active", new Date()).set("last_active_int", times).save();
            }
        } else {
            online.set("last_login", new Date()).set("login_ip", ip)
                    .set("last_active", new Date()).set("last_active_int", times).update();
        }

    }

    /**
     * 记录最后活动时间
     */
    public void saveActive(String sessid) {
        long b = StringUtil.getLong(DOTIME, sessid);
        long e = System.currentTimeMillis();
        if (e - b > 1 * 60 * 1000) {//1分钟记录一次
            SysOnline online = SysOnline.dao.findById(sessid);
            long times = DateUtil.getYYYYMMHHMMSS();
            if (online != null) {
                online.set("last_active", new Date()).set("last_active_int", times).update();
            }
            DOTIME.put(String.valueOf(sessid), e);
        }
    }

    /**
     * 统计在线人数
     *
     * @return
     */
    public String getOnlineUserInfo() {
        Date date_15 = DateUtil.getAddDate(new Date(), -15 * 60 * 1000L);
        Date date_60 = DateUtil.getAddDate(new Date(), -60 * 60 * 1000L);
        Date date_360 = DateUtil.getAddDate(new Date(), -360 * 60 * 1000L);
        Date date_1440 = DateUtil.getAddDate(new Date(), -1440 * 60 * 1000L);
        int user_15 = Db.queryInt("select count(*) from sys_online where last_active >?", date_15);
        int user_60 = Db.queryInt("select count(*) from sys_online where last_active >?", date_60);
        int user_360 = Db.queryInt("select count(*) from sys_online where last_active >?", date_360);
        int user_1440 = Db.queryInt("select count(*) from sys_online where last_active >?", date_1440);
        return "15分钟在线<span>" + user_15 + "</span>人，1小时在线<span>" + user_60
                + "</span>人，6小时在线<span>" + user_360 + "</span>人，24小时在线<span>" + user_1440 + "</span>人";
    }
}
